package mulMan.gui.controller;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;
import java.util.Observable;
import mulMan.domain.AudioFileModel;
import mulMan.gui.view.AudioFilesView;
import mulMan.gui.view.ModifyFileView;

public class AudioFilesController extends Observable{
	
	private List<AudioFileModel> audioFiles;
	private AudioFilesView view;
	
	
	public List<AudioFileModel> getAudiosFile() {
		return audioFiles;
	}
	public void setAudioFiles(List<AudioFileModel> audioFiles) {
		this.audioFiles = audioFiles;
	}

	public AudioFilesView getView() {
		return view;
	}
	public void setView(AudioFilesView view) {
		this.view = view;
	}
	
	
	public AudioFilesController(List<AudioFileModel> audioFiles, AudioFilesView view) {
		this.audioFiles = audioFiles;
		this.view = view;
		
		view.addTableAudioFilesListener(new TableAudioFilesListener());
	}
	
	
	//ACTION LISTENERS
	class TableAudioFilesListener implements MouseListener {

		public void mouseClicked(MouseEvent e) {
			if(e.getClickCount() == 2){
				
	            new AudioFileController(audioFiles.get(view.getTableOfAudioFiles().getSelectedRow()), 
	            		new ModifyFileView(audioFiles.get(view.getTableOfAudioFiles().getSelectedRow())));
			}
		}

		public void mouseEntered(MouseEvent e) {}
		public void mouseExited(MouseEvent e) {}
		public void mousePressed(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}
		public void mouseReleased(MouseEvent e) {
			if (e.isPopupTrigger()) {
				showPopup(e);
			}
		}

		private void showPopup(MouseEvent e) {
			view.getTableOfAudioFiles().getSelectionModel().setLeadSelectionIndex(view.getTableOfAudioFiles().rowAtPoint(e.getPoint()));
			view.getPopupMenu().show(view.getTableOfAudioFiles(), e.getX(), e.getY());
		}
    }
}
